package net.kleinhenz.questor.model;

import java.util.HashMap;
import java.util.Map;

import org.simpleframework.xml.ElementMap;

public abstract class VariableContainer
{
	// String encoding of variables is used because of the XML serializing 
	@ElementMap(required=false, entry="param", key="key", attribute=true, inline=true)
	protected Map<String, String> variables = new HashMap<String, String>();

	public void setVariables(Map<String, String> variables) {
		this.variables = variables;
	}
	
	public Map<String, String> getVariables() {
		return variables;
	}
	
	public void setVariable(String key, String value) {
		this.variables.put(key, value);
	}

	public void setVariable(String key, Integer value) {
		this.variables.put(key, "" + value);
	}

	public void setVariable(String key, Boolean value) {
		if (value==null)
			throw new RuntimeException("Boolean value is null.");
		this.variables.put(key, value.toString());
	}

	public void setVariable(String key, Double value) {
		this.variables.put(key, "" + value);
	}

	public String getVariableString(String key) {
		return getVariables().get(key);
	}
	
	public Integer getVariableInteger(String key) {
		return Integer.parseInt(getVariableString(key));
	}
	
	public Boolean getVariableBoolean(String key) {
		return Boolean.parseBoolean(getVariableString(key));		
	}
	
	public double getVariableDouble(String key) {
		return Double.parseDouble(getVariableString(key));		
	}

	public void removeVariable(String key)
	{
		getVariables().remove(key);
	}

	public boolean variableExists(String key)
	{
		return getVariables().containsKey(key);
	}
}